package com.groupbuying.settleconsumer.service.impl.settleType;

import com.groupbuying.settleconsumer.common.ChannelServiceStrategyFactory;
import com.groupbuying.settleconsumer.common.SettleServiceStrategyFactory;
import com.groupbuying.settleconsumer.domain.SettleUshopSplitRluesDO;
import com.groupbuying.settleconsumer.service.ChannelService;
import com.groupbuying.settleconsumer.service.SettleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 优惠买单策略类
 * author cuich
 * date 2019/12/2 10:31 上午
 */
@Service
public class UshopYhmdSettleService implements SettleService, InitializingBean {

    public static final Logger logger = LoggerFactory.getLogger(UshopYhmdSettleService.class);

    @Override
    public SettleUshopSplitRluesDO getRule(Map params) {
        String orderId = (String) params.get("orderId");//订单号
        String storeId = (String) params.get("storeId");
        String settleRuleId = (String) params.get("settleRuleId");
        String payChannel = (String) params.get("payChannel");
        //商户的平台抽成费率
        String storeRate = (String) params.get("storeRate");
        //查询优惠规则表
        BigDecimal backCurrencyRatio ;//返积分比例
        BigDecimal platformRatio ;//平台抽成比例
        //根据支付渠道和支付类型，设置通道费和平台补贴费
        BigDecimal channleRatio ;//通道费费率
        BigDecimal subsidiesRatio ;//平台补贴费费率
        ChannelService channelService = ChannelServiceStrategyFactory.getByChannelType(payChannel);
        Map<String,BigDecimal> channelRatios = channelService.getChannelRate();
        channleRatio = channelRatios.get("channleRatio");
        subsidiesRatio = channelRatios.get("subsidiesRatio");
        //商户结算比例
        BigDecimal customerRatio;
        try{
            //优惠买单规则
            platformRatio = StringUtils.isEmpty(storeRate)? new BigDecimal("0.00"):new BigDecimal(storeRate);
            backCurrencyRatio = new BigDecimal("0.00");
            customerRatio = new BigDecimal("1.00").subtract(platformRatio.add(backCurrencyRatio).add(channleRatio)).add(subsidiesRatio);

            //分账规则表中ruleId与优惠规则表和抽成规则表没有关系，会自己生成ruleId,并与分账主表做关联
            SettleUshopSplitRluesDO settleUshopSplitRluesDO = new SettleUshopSplitRluesDO();
            settleUshopSplitRluesDO.setCustomerId(storeId);
            settleUshopSplitRluesDO.setRluesId(settleRuleId);
            settleUshopSplitRluesDO.setVirtualRatio(backCurrencyRatio);
            settleUshopSplitRluesDO.setPlatformRatio(platformRatio);

            settleUshopSplitRluesDO.setChannleRatio(channleRatio);
            settleUshopSplitRluesDO.setSubsidiesRatio(subsidiesRatio);

            settleUshopSplitRluesDO.setCustomerRatio(customerRatio);
            settleUshopSplitRluesDO.setIsDelete("1");
            settleUshopSplitRluesDO.setGmtCreate(new Date());
            logger.error("OrderSettlementServiceImpl-->initializeRuleParams return SettleSplitRluesDO:"+settleUshopSplitRluesDO);
            return settleUshopSplitRluesDO;
        }catch (Exception e){
            logger.error("UshopYhmdSettleService-->initializeRuleParams "+orderId+" sysconfigFeign fail",e);
            return null;
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        SettleServiceStrategyFactory.register("UshopYhmd",this);
    }
}
